package com.cskaoyan.service.wx_home;


import com.cskaoyan.bean.configmanagement.WX;
import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.mall.brand.BrandExample;
import com.cskaoyan.bean.wx_goods.CategoryExample;
import com.cskaoyan.bean.promote.AdvertisingExample;
import com.cskaoyan.bean.promote.CouponExample;
import com.cskaoyan.bean.promote.TopicExample;
import com.cskaoyan.bean.wx_goods.Groupon;
import com.cskaoyan.bean.wx_goods.GrouponExample;
import com.cskaoyan.bean.wx_home.*;
import com.cskaoyan.mapper.configmanagement.ConfigManagementMapper;
import com.cskaoyan.mapper.goods.GoodsMapper;
import com.cskaoyan.mapper.mall.BrandMapper;
import com.cskaoyan.mapper.wx_goods.WxCategoryMapper;
import com.cskaoyan.mapper.promote.AdvertisingMapper;
import com.cskaoyan.mapper.promote.CouponMapper;
import com.cskaoyan.mapper.promote.TopicMapper;
import com.cskaoyan.mapper.wx_groupon.GrouponMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: enhui
 **/
@Service
public class WXHomeServiceImpl implements WXHomeService {
    @Autowired
    ConfigManagementMapper configMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    WxCategoryMapper categoryMapper;
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    AdvertisingMapper advertisingMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;

    @SneakyThrows
    @Override
    public WxHomeIndexVO index() {
        WxHomeIndexVO homeIndexVO = new WxHomeIndexVO();
        WX wx = selectValueByFields(new WX());

        //新品首发栏目商品 查询goods表 isNew的，后台查询设置的数量
        Integer newNum = Integer.valueOf(wx.getMarket_wx_index_new());
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        goodsExampleCriteria.andIsNewEqualTo(true);
        goodsExample.setOrderByClause("id desc limit " + newNum);
        List<WxGoods> wxNewGoods = goodsMapper.selectWXGoodsByExample(goodsExample);
        homeIndexVO.setNewGoodsList(wxNewGoods);

        //查coupon表 优惠券个数暂时设置为3
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause("discount desc limit 3");
        List<WxCoupon> coupons = couponMapper.selectWXCouponByExample(couponExample);
        homeIndexVO.setCouponList(coupons);

        //查category表 L1的
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andLevelEqualTo("L1");
        List<WxChannel> WxChannels = categoryMapper.selectWXCategoryByExample(categoryExample);
        homeIndexVO.setChannel(WxChannels);

        //查groupon_rule表，先按时间逆序限制两个
//        GrouponExample grouponExample = new GrouponExample();
//        grouponExample.setOrderByClause("expire_time desc limit 2");
//        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
//        homeIndexVO.setGrouponList(groupons);

        //查ad表 排个序吧，最新的放前面
        AdvertisingExample advertisingExample = new AdvertisingExample();
        advertisingExample.setOrderByClause("id desc");
        List<WxBanner> wxBanners = advertisingMapper.selectWXBannerByExample(advertisingExample);
        homeIndexVO.setBanner(wxBanners);

        //品牌制造商直供品牌商数量  查品牌brand表 后台设置数量
        Integer brandNum = Integer.valueOf(wx.getMarket_wx_index_brand());
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause("id limit " + brandNum);
        List<WxBrand> wxBrands = brandMapper.selectWXBrandByExample(brandExample);
        homeIndexVO.setBrandList(wxBrands);

        //人气推荐栏目商品 查询goods表 isHot的 后台设置数量
        Integer hotNum = Integer.valueOf(wx.getMarket_wx_index_hot());
        GoodsExample hotGoodsExample = new GoodsExample();
        GoodsExample.Criteria hotGoodsExampleCriteria = hotGoodsExample.createCriteria();
        hotGoodsExampleCriteria.andIsHotEqualTo(true);
        hotGoodsExample.setOrderByClause("id desc limit " + hotNum);
        List<WxGoods> wxHotGoods = goodsMapper.selectWXGoodsByExample(hotGoodsExample);
        homeIndexVO.setHotGoodsList(wxHotGoods);

        //专题精选栏目 查topic表 后台设置数量
        Integer topicNum = Integer.valueOf(wx.getMarket_wx_index_topic());
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause("read_count desc limit " + topicNum);
        List<WxTopic> wxTopics = topicMapper.selectWXTopicByExample(topicExample);
        homeIndexVO.setTopicList(wxTopics);

        //底部显示goods根据category查 后台设置数量
        ArrayList<WxFloorGoodsList> wxFloorGoodsLists = new ArrayList<>();
        Integer categoryNum = Integer.valueOf(wx.getMarket_wx_catlog_list());
        Integer categoryGoodsNum = Integer.valueOf(wx.getMarket_wx_catlog_goods());
        //需要判断后台设置的这个类目如果大于总的分类的类别，最多显示总的类别
        //避免数组越界异常
        if (categoryNum > WxChannels.size()) {
            categoryNum = WxChannels.size();
        }
        for (int i = 0; i < categoryNum; i++) {
            WxFloorGoodsList wxFloorGoodsList = new WxFloorGoodsList();
            //显示的是分类栏目的一级目录
            WxChannel wxChannel = WxChannels.get(i);
            Integer wxChannelId = wxChannel.getId();
            wxFloorGoodsList.setId(wxChannelId);
            wxFloorGoodsList.setName(wxChannel.getName());
            //根据category的一级目录查到二级的categories
            CategoryExample floorCategoryExample = new CategoryExample();
            CategoryExample.Criteria criteria = floorCategoryExample.createCriteria();
            criteria.andPidEqualTo(wxChannelId);
            List<WxChannel> categories = categoryMapper.selectWXCategoryByExample(floorCategoryExample);
            //根据二级分类查询下面的商品，二级的放在一个list中
            List<Integer> categoryIdList = new ArrayList<>();
            for (WxChannel category : categories) {
                Integer categoryId = category.getId();
                categoryIdList.add(categoryId);
            }
            GoodsExample floorGoodsExample = new GoodsExample();
            floorGoodsExample.setOrderByClause("id desc limit " + categoryGoodsNum);
            GoodsExample.Criteria floorGoodsExampleCriteria = floorGoodsExample.createCriteria();
            floorGoodsExampleCriteria.andCategoryIdIn(categoryIdList);
            List<WxGoods> goodsList = goodsMapper.selectWXGoodsByExample(floorGoodsExample);
            wxFloorGoodsList.setGoodsList(goodsList);
            wxFloorGoodsLists.add(wxFloorGoodsList);
        }
        homeIndexVO.setFloorGoodsList(wxFloorGoodsLists);
        return homeIndexVO;
    }

    private <T> T selectValueByFields(T t) throws IllegalAccessException {
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            String value = configMapper.selectByName(name);
            declaredField.set(t, value);
        }
        return t;
    }

    //利用反射赋值
//    @SneakyThrows
//    private static <T,E> void setVOFields(T fromBean, E targetBean) {
//        Class<?> fromClazz = fromBean.getClass();
//        Field[] fromFields = fromClazz.getDeclaredFields();
//        Class<?> targetClazz = targetBean.getClass();
//        Field[] targetFields = targetClazz.getDeclaredFields();
//
//        for (Field targetField : targetFields) {
//            String targetFieldName = targetField.getName();
//            targetField.setAccessible(true);
//            for (Field fromField : fromFields) {
//                String fromFieldName = fromField.getName();
//                fromField.setAccessible(true);
//                Object fromValue = fromField.get(fromBean);
//                if (targetFieldName.equals(fromFieldName)) {
//                    targetField.set(targetBean, fromValue);
//                }
//            }
//        }
//    }
}